{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "balance = int(input(\"请输入您的支付宝余额：\"))\n",
    "products = [(\"iPhone 12 Mini\", 699),\n",
    "            (\"iPhone 12\", 799),\n",
    "            (\"iPhone 12 Pro\", 999),\n",
    "            (\"iPhone 12 Pro Max\", 1099)]\n",
    "cart = {}\n",
    "\n",
    "while True:\n",
    "    # 打印商品目录\n",
    "    for product in products:\n",
    "        print(products.index(product) + 1, product[0], product[1])\n",
    "    selection = int(input(\"输入商品编号进行购买：\"))\n",
    "    # 判断编号输入正确与否\n",
    "    if 1 <= selection < len(products) + 1:\n",
    "        count = int(input(\"输入购买数量：\"))\n",
    "        # 判断余额是否足以购买\n",
    "        if balance >= products[selection - 1][1] * count:\n",
    "            # 记录商品名称及购买数量\n",
    "            cart.update({products[selection - 1][0]: count})\n",
    "            # 更新余额\n",
    "            balance -= products[selection - 1][1] * count\n",
    "            print(\"购买成功，支付宝余额剩余{}。\".format(balance))\n",
    "            continueShopping = input(\"是否继续购物：\")\n",
    "            if continueShopping == \"否\":\n",
    "                # 输出购买详情及最终余额\n",
    "                print(\"您共购买了\", end=\"\")\n",
    "                for key, value in cart.items():\n",
    "                    print(\"{}台{}；\".format(value, key), end=\"\")\n",
    "                print(\"支付宝余额剩余{}。\".format(balance))\n",
    "                break\n",
    "        else:\n",
    "            print(\"余额不足。\")\n",
    "            continueShopping = input(\"是否继续购物：\")\n",
    "            if continueShopping == \"否\":\n",
    "                # 输出购买详情及最终余额\n",
    "                print(\"您共购买了\", end=\"\")\n",
    "                for key, value in cart.items():\n",
    "                    print(\"{}台{}；\".format(value, key), end=\"\")\n",
    "                print(\"支付宝余额剩余{}。\".format(balance))\n",
    "                break\n",
    "                else:\n",
    "        print(\"无此商品，请重新选择。\", \"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
